#ifndef GULLENGINE_GUID_H
#define GULLENGINE_GUID_H

#include <map>

namespace gull{
    namespace core {
        
        class GUID
        {
        public:
            
            inline static unsigned int getGUID();
            inline static void freeGUID(unsigned int);
            
        private:
            static std::map<unsigned int, bool> s_guiSet;
        };       
        
       
        
        unsigned int 
        GUID::getGUID()
        {
            if(s_guiSet.empty())
                s_guiSet.insert(std::pair<unsigned int, bool>(0,false));
            
            std::map<unsigned int, bool>::iterator it = s_guiSet.begin();
            while (it != s_guiSet.end()) 
            {
                if((*it).second == false)
                {
                    (*it).second = true;
                    return (*it).first;
                }
                it++;
            }
            --it;
            std::pair<unsigned int, bool> p((*it).first+1, true);
            s_guiSet.insert(p);
            return p.first;
        }
        
        void
        GUID::freeGUID(unsigned int guid)
        {
           (*s_guiSet.find(guid)).second = false;
        }
    }
}
#endif